﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StoredProcedure
{
    public partial class Form1 : Form
    {
        const string ConStr = "Data Source=.;Initial Catalog=ADO_NET_Simple;Integrated Security=True";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnNoParameter_Click(object sender, EventArgs e)
        {
            SqlDataAdapter da = new SqlDataAdapter("usp_selectStudents", ConStr);
            //用存储过程来解释命令字符串
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            da.Fill(dt);
            dgvStudents.DataSource = dt;
            lblOutPut.Text = "";
            lblResult.Text = "";
        }

        private void btnInput_Click(object sender, EventArgs e)
        {
            SqlDataAdapter da = new SqlDataAdapter("usp_selectStudents", ConStr);
            //用存储过程来解释命令字符串
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            //设置参数，和参数化查询一致
            da.SelectCommand.Parameters.Add(new SqlParameter("name", "周"));
            DataTable dt = new DataTable();
            da.Fill(dt);
            dgvStudents.DataSource = dt;
            lblOutPut.Text = "";
            lblResult.Text = "";
        }

        private void btnOutPut_Click(object sender, EventArgs e)
        {
            SqlDataAdapter da = new SqlDataAdapter("usp_selectStudents", ConStr);
            //用存储过程来解释命令字符串
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            //设置参数，和参数化查询一致
            da.SelectCommand.Parameters.Add(new SqlParameter("name", "周"));
            da.SelectCommand.Parameters.Add(new SqlParameter("sumAge",SqlDbType.Int));
            //指定参数是输出参数
            da.SelectCommand.Parameters["sumAge"].Direction = ParameterDirection.Output;
            DataTable dt = new DataTable();
            da.Fill(dt);
            dgvStudents.DataSource = dt;
            //获取输出参数
            object outPut = da.SelectCommand.Parameters["sumAge"].Value;
            lblOutPut.Text = "输出参数是：" + outPut;
            lblResult.Text = "";
        }

        private void btnResult_Click(object sender, EventArgs e)
        {
            SqlDataAdapter da = new SqlDataAdapter("usp_selectStudents", ConStr);
            //用存储过程来解释命令字符串
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            //设置参数，和参数化查询一致
            da.SelectCommand.Parameters.Add(new SqlParameter("name", "周"));
            da.SelectCommand.Parameters.Add(new SqlParameter("sumAge", SqlDbType.Int));
            da.SelectCommand.Parameters.Add(new SqlParameter("count", SqlDbType.Int));
            //指定参数是输出参数
            da.SelectCommand.Parameters["sumAge"].Direction = ParameterDirection.Output;
            //指定参数是返回值参数
            da.SelectCommand.Parameters["count"].Direction = ParameterDirection.ReturnValue;
            DataTable dt = new DataTable();
            da.Fill(dt);
            dgvStudents.DataSource = dt;
            //获取输出参数
            object outPut = da.SelectCommand.Parameters["sumAge"].Value;
            //获取返回值参数
            object result = da.SelectCommand.Parameters["count"].Value;
            lblOutPut.Text = "输出参数是：" + outPut;
            lblResult.Text = "返回值是是：" + result;
        }
    }
}
